﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Expo.Training.Entity;
using System.IO;
using Expo.Training.BRule;

public partial class Ad_AddAds : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.txt_BegDate.Text = DateTime.Now.ToShortDateString();
            this.txt_EndDate.Text = DateTime.Now.ToShortDateString();
        }
    }

    public void DoAdd(object sender, System.EventArgs e)
    {
        #region [ 添加广告 ]
        sp_title_error.InnerText = "";
        string title = HttpUtility.HtmlEncode(this.txt_Title.Text.Trim());
        string url=HttpUtility.HtmlEncode(this.txt_url.Text.Trim());
        int Adheight = 0;
        int Adwidth = 0;
        DateTime begDate = DateTime.Parse(this.txt_BegDate.Text);
        DateTime endDate = DateTime.Parse(this.txt_EndDate.Text);

        if (title.Length == 0)
        {
            sp_title_error.InnerText = "标题不能为空！";
            return;
        }

        if (begDate.ToString().Length == 0)
        {
            txt_BgnDate_error.InnerText = "开始时间不能为空！";
            return;
        }

        if (endDate.ToString().Length == 0)
        {
            txt_EndDate_error.InnerText = "结束时间不能为空！";
            return;
        }

        Advertisement objAds = new Advertisement();
        objAds.Title = title;
        objAds.Url =url;
        Guid user = this.Member.MemberID;
        objAds.CreatorID = user;
        objAds.Position=this.ddlPosition.SelectedValue;
        objAds.Vesion=this.ddlVesion.SelectedValue;
        objAds.CreateDate=DateTime.Now;
        objAds.BeginDate = begDate;
        objAds.EndDate = endDate;
        string folderName = ResolveClientUrl("~/Data/Ads/");
        if (this.ddlVesion.SelectedValue == "0")
        {
            Adwidth = 1024;
            Adheight = 69;
        }
        else if (this.ddlVesion.SelectedValue == "1")
        {
            Adwidth = 216;
            Adheight = 89;
        }
        else if (this.ddlVesion.SelectedValue == "2")
        {
            Adwidth = 127;
            Adheight = 140;
        }
        else if (this.ddlVesion.SelectedValue == "3")
        {
            Adwidth = 127;
            Adheight = 130;
        }
        else
        {
            Adwidth = 1024;
            Adheight = 69;
        }
        objAds.Adwidth = Adwidth;
        objAds.Adheight = Adheight;
        if (chkVisble.Checked)
            objAds.IsVisble = 1;
        if (!Directory.Exists(Server.MapPath(folderName)))
        {
            Directory.CreateDirectory(Server.MapPath(folderName));
        }
        string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
        string fileExtension = Path.GetExtension(this.File1.PostedFile.FileName).ToUpper();
        string filePath = Server.MapPath(folderName + fileName + fileExtension);
        if (fileExtension != "" || fileExtension == null)
        {
            if (!(fileExtension == ".JPEG" || fileExtension == ".JPG" || fileExtension == ".PNG" || fileExtension == ".GIF"))
            {
                this.AddEndScript("error", "alert('不支持的图片格式！');");
                return;
            }
            this.File1.PostedFile.SaveAs(filePath);
            CutImage image = new CutImage();
            image.CutImageBySize(filePath, Server.MapPath(folderName + fileName + "_n" + fileExtension), Adwidth, Adheight);
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            objAds.PictureUrl =   fileName + "_n" + fileExtension;
        }
        objAds.Tag = 0;
        if(objAds.Add())
        {
            Expo.Training.BRule.Audit.Audit.AuditLog(this.Member.MemberID, "广告管理 - 添加 - ID:" +objAds.Title );
            Response.Redirect("Default.aspx");
        }
        objAds.Dispose();
        
        #endregion
    }
}
